<template>
  <div class="index-lists">
    <el-card class="!border-none" shadow="never">
      <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
        <el-form-item label="" prop="id" v-if="false">
          <el-input class="w-[280px]" v-model="queryParams.id" />
        </el-form-item>
        <el-form-item label="智书id" prop="bookId">
          <el-input class="w-[280px]" v-model="queryParams.bookId" />
        </el-form-item>
        <el-form-item label="标注编辑账号id" prop="markUserId" v-if="false">
          <el-input class="w-[280px]" v-model="queryParams.markUserId" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="!border-none mt-4" shadow="never">
      <div v-if="false">
        <el-button v-perms="['wisdbook:add']" type="primary" @click="handleAdd()">
          <template #icon>
            <icon name="el-icon-Plus" />
          </template>
          新增
        </el-button>
      </div>
      <el-table class="mt-4" size="large" v-loading="pager.loading" :data="pager.lists">

        <el-table-column label="智书id" prop="bookId" min-width="100" />
        <el-table-column label="书名" prop="name" min-width="100" />
        <el-table-column label="年级" prop="grade" min-width="100">
          <template #default="{ row }">
            <dict-value :options="dictData.年级" :value="row.grade" />
          </template>
        </el-table-column>
        <el-table-column label="科目" prop="subject" min-width="100">
          <template #default="{ row }">
            <dict-value :options="dictData.科目" :value="row.subject" />
          </template>
        </el-table-column>

        <el-table-column label="标注状态" prop="markStatus" min-width="100">
          <template #default="{ row }">
            <dict-value :options="dictData.markStatus" :value="row.markStatus" />
          </template>
        </el-table-column>

        <el-table-column label="标注员" prop="nickname" min-width="100" />
        
        <el-table-column label="父书id" prop="pid" min-width="100" />

        <el-table-column label="操作" width="220" fixed="right">
          <template #default="{ row }">
            <el-button v-perms="['wisdbook:user:edit']" type="primary" link @click="handleMark(row)">
              标注
            </el-button>

            <el-button v-if="row.markStatus!=2" v-perms="['wisdbook:user:edit']" type="primary" link @click="onClickMarkFinish(row,2)">
              标注完成
            </el-button>


            <el-button v-if="row.markStatus==2" v-perms="['wisdbook:user:edit']"  type="primary" link @click="onClickMarkFinish(row,1)">
              取消标注完成
            </el-button>

            <el-button  type="primary" :disabled="row.markStatus!=2" link @click="onClickHangQuest(row)" v-if="false">
              题目挂接
            </el-button>

          </template>
        </el-table-column>
      </el-table>
      <div class="flex justify-end mt-4">
        <pagination v-model="pager" @change="getLists" />
      </div>
    </el-card>
    <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />



    <el-dialog
    v-model="isMarkDlgVisible"
    title="Tips"
    width="30%"
  >
    <span>确定标注完成了？</span>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="isMarkDlgVisible = false">取消</el-button>
        <el-button type="primary" @click="onClickMarkFinishSure()">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>

  </div>
</template>
<script lang="ts" setup name="user">
import { userDelete, userLists, listByUser,wisdbookEdit } from '@/api/wisdbook'
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from "@/hooks/useDictOptions";
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
const router = useRouter()
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
  id: '',
  bookId: '',
  markUserId: '',
})

//mark finish
const isMarkDlgVisible = ref(false)
let lastRow:any={};
let lastMarkStatus = 0;
const onClickMarkFinish = (row:any,markStatus:any)=>{
  lastRow = row
  console.log('onClickMarkFinish',row);
  lastMarkStatus = markStatus;
  isMarkDlgVisible.value = true

}
const onClickMarkFinishSure = async ()=>{
 
  await wisdbookEdit({id:lastRow.bookId,markStatus:lastMarkStatus,pid:lastRow.pid}) 
  let find=pager.lists.filter(r=>r.bookId==lastRow.bookId)
  find[0].markStatus=lastMarkStatus
  isMarkDlgVisible.value = false
}
//end mark finish



const onClickHangQuest = (row:any)=>{
  console.log('onClickHangQuest',row.bookId);
  
  router.push('/hang_quest/index?bookId='+row.bookId)
}

const { pager, getLists, resetPage, resetParams } = usePaging({
  fetchFun: listByUser,
  params: queryParams
})

const { dictData } = useDictData<{
  年级: any[];
  科目: any[];
  学段: any[];
  版本: any[];
  分册: any[];
  markStatus: any[];
  addStacks: any[];
}>(["年级", "科目", "学段", "版本", "分册", "markStatus", "addStacks"]);


const handleAdd = async () => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('edit')
  editRef.value?.getDetail(data)
}

const handleMark = async (data: any) => {

  router.push('/ebook/marker?bookId='+data.bookId)
}


const handleDelete = async (id: number) => {
  await feedback.confirm('确定要删除？')
  await userDelete({ id })
  feedback.msgSuccess('删除成功')
  getLists()
}

getLists()
</script>
